home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ViewTester / Sources / Dialog.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  7.3 KB  |  279 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Dialog.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef DIALOG_H
  11. #include "Dialog.h"
  12. #endif
  13.  
  14. #ifndef PART_H
  15. #include "Part.h"
  16. #endif
  17.  
  18. #ifndef FRAME_H
  19. #include "Frame.h"
  20. #endif
  21.  
  22. #ifndef DEFINES_K
  23. #include "Defines.k"
  24. #endif
  25.  
  26. // ----- Framework Layer -----
  27.  
  28. #ifndef FWEDVIEW_H
  29. #include "FWEdView.h"
  30. #endif
  31.  
  32. #ifndef FWSTATIC_H
  33. #include "FWStatic.h"
  34. #endif
  35.  
  36. #ifndef FWUTIL_H
  37. #include "FWUtil.h"
  38. #endif
  39.  
  40. #ifndef FWCONTXT_H
  41. #include "FWContxt.h"
  42. #endif
  43.  
  44. #ifndef FWBUTTON_H
  45. #include "FWButton.h"
  46. #endif
  47.  
  48. #ifndef FWNOTDEF_H
  49. #include "FWNotDef.h"
  50. #endif
  51.  
  52. #ifndef FWPOPUP_H
  53. #include "FWPopup.h"
  54. #endif
  55.  
  56. // ----- OS Layer -----
  57.  
  58. #ifndef FWMENU_H
  59. #include "FWMenu.h"
  60. #endif
  61.  
  62. #ifndef FWEVENT_H
  63. #include "FWEvent.h"
  64. #endif
  65.  
  66. #ifndef FWALERT_H
  67. #include "FWAlert.h"
  68. #endif
  69.  
  70. #ifndef SLMixOS_H
  71. #include "SLMixOS.h"
  72. #endif
  73.  
  74. #ifndef FWCFMRES_H
  75. #include "FWCFMRes.h"
  76. #endif
  77.  
  78. // ----- Foundation Layer -----
  79.  
  80. #ifndef FWEXCDEF_H
  81. #include <FWExcDef.h>
  82. #endif
  83.  
  84. #ifndef FWNOTIFN_H
  85. #include "FWNotifn.h"
  86. #endif
  87.  
  88. // ----- PPob View Support -----
  89.  
  90. #if FW_PPOB_VIEWS
  91. #include "FWPPobRd.h"
  92. #endif
  93.  
  94. // ----- MacApp View Support -----
  95.  
  96. #if FW_MACAPP_VIEWS
  97. #include "FWMARead.h"
  98. #endif
  99.  
  100. //========================================================================================
  101. // Runtime Informations
  102. //========================================================================================
  103.  
  104. #ifdef FW_BUILD_MAC
  105. #pragma segment viewtester
  106. #endif
  107.  
  108. FW_DEFINE_AUTO(CDialogFrame)
  109.  
  110. //========================================================================================
  111. // class CDialogFrame 
  112. //========================================================================================
  113.  
  114. //----------------------------------------------------------------------------------------
  115. // CDialogFrame::CDialogFrame
  116. //----------------------------------------------------------------------------------------
  117. CDialogFrame::CDialogFrame(Environment* ev, ODFrame* odFrame, FW_CPresentation* presentation,    
  118.                         CViewTesterPart* part)
  119.     : FW_CDialogFrame(ev, odFrame, presentation, part) 
  120. {
  121. }
  122.  
  123. //----------------------------------------------------------------------------------------
  124. // CDialogFrame::~CDialogFrame
  125. //----------------------------------------------------------------------------------------
  126.  
  127. CDialogFrame::~CDialogFrame()
  128. {
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. // CDialogFrame::Initialize
  133. //----------------------------------------------------------------------------------------
  134.  
  135. void CDialogFrame::Initialize(Environment* ev)
  136. {
  137.     FW_Boolean usingMacApp = ((CViewTesterPart*)GetPart(ev))->UsingMacApp();
  138.  
  139.     ODID editID = usingMacApp ?  kMacAppEditID : kPPobEditID;
  140.     ODID otherEditID = usingMacApp ?  kPPobEditID : kMacAppEditID;
  141.     ODID otherPopupID = usingMacApp ? kPPobPopupID : kMacAppPopupID;
  142.  
  143.     FW_CView* view =  FindViewByID(ev, editID);
  144.     view->BecomeTarget(ev, false);
  145.     view =  FindViewByID(ev, otherEditID);
  146.     view->SetVisible(ev, false);
  147.     view =  FindViewByID(ev, otherPopupID);
  148.     view->Disable(ev);
  149.     
  150.     // Make sure that classes created from ODFRC resources won't be dead-stripped
  151.     FW_DO_NOT_DEAD_STRIP(FW_CStaticText);
  152. }
  153.  
  154. //----------------------------------------------------------------------------------------
  155. //     CDialogFrame::Draw
  156. //----------------------------------------------------------------------------------------
  157. /*
  158. void CDialogFrame::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  159. {
  160.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  161.  
  162.     // Dialog needs to erase its background in order to hide the edit views after
  163.     // using Invalidate().
  164.     FW_CRect rect;
  165.     vc.GetClipRect(rect);    
  166.     FW_CRectShape::RenderRect(vc, rect, FW_kFill, FW_kWhiteEraseInk);
  167. }
  168. */
  169. //----------------------------------------------------------------------------------------
  170. //    CDialogFrame::GetResourceID
  171. //----------------------------------------------------------------------------------------
  172.  
  173. short CDialogFrame::GetResourceID(Environment* ev, const FW_CString& resName)
  174. {
  175.     short id = 0;
  176.     Str255 pName;
  177.     resName.ExportPascal(pName);
  178.     
  179.     // Open the current resource file and find the id of the selected resource 
  180.     // (because ODF doesn't any API to open a resource by name...)
  181.     
  182.     FW_PResourceFile* resFile = 0;
  183.     long resType;
  184.     
  185.     if (((CViewTesterPart*)GetPart(ev))->UsingMacApp())
  186.     {
  187.         resType = FW_kMAType;
  188.         resFile = FW_CMacAppReader::GetResFile(ev, FW_CMacAppReader::kCurrentResFile);
  189.     }
  190.     else
  191.     {
  192.         resType = FW_kPPobType;
  193.         resFile = FW_CPPobReader::GetResFile(ev, FW_CPPobReader::kCurrentResFile);
  194.     }
  195.     
  196.     Handle resHdl = Get1NamedResource(resType, pName);
  197.     if (resHdl)
  198.     {
  199.         ResType type;
  200.         GetResInfo(resHdl, &id, &type, pName);
  201.     }
  202.     else
  203.         FW_Beep();        // shouldn't happen
  204.     
  205.     delete resFile;
  206.     return id;
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. //    CDialogFrame::HandleNotification
  211. //----------------------------------------------------------------------------------------
  212.  
  213. void CDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
  214. {
  215.     long viewID = 0;
  216.     CViewTesterPart* part = (CViewTesterPart*)GetPart(ev);
  217.     ODID editID = part->UsingMacApp() ? kMacAppEditID : kPPobEditID;
  218.     ODID popupID = part->UsingMacApp() ? kMacAppPopupID : kPPobPopupID;
  219.     FW_CEditView* editView =  FW_DYNAMIC_CAST(FW_CEditView, FindViewByID(ev, editID));
  220.     
  221.     FW_Message dialogMsg = notification.GetMessage();
  222.  
  223.     if (dialogMsg ==  FW_kDefaultButtonMsg) 
  224.     {
  225.         viewID = editView->GetText(ev).ParseAsUnsignedInteger();
  226.     } 
  227.     else if (dialogMsg == FW_kPopupClickedMsg)
  228.     {
  229.         const FW_CPopupMenuNotification& popupNotification = 
  230.                                                 (FW_CPopupMenuNotification&) notification;
  231.         // update the edit view
  232.         if (popupNotification.GetMenuIndex(ev) == 1)
  233.         {
  234.             editView->SetText(ev, FW_CString());
  235.             editView->Enable(ev);    
  236.             editView->BecomeTarget(ev, false);    
  237.         }
  238.         else 
  239.         {
  240.             FW_CString resStr;
  241.             popupNotification.GetMenuString(ev, resStr);    
  242.             viewID = GetResourceID(ev, resStr);
  243.             resStr.ReplaceAllAsUnsignedDecimalInteger(viewID);
  244.  
  245.             editView->ResignTarget(ev);
  246.             editView->Disable(ev);    
  247.             editView->SetText(ev, resStr);
  248.         }
  249.     }
  250.     
  251.     if (dialogMsg ==  FW_kDefaultButtonMsg || dialogMsg ==  FW_kCancelButtonMsg) 
  252.     {
  253.         // MEGA HACK to work around a Mac ToolBox problem: if we don't purge the menu
  254.         // handle from its additional resource items everytime the dialog is closed,
  255.         // then the menu duplicates them the next time the dialog is up!
  256.         // (can someone explain me why?)
  257.         FW_CPopupMenu* popup =  (FW_CPopupMenu*)FindViewByID(ev, kMacAppPopupID);
  258.         MenuHandle menuHdl = (MenuHandle)popup->GetPlatformMenuHandle();
  259.         short numItems = CountMItems(menuHdl);
  260.         int i;
  261.         for (i = numItems; i > 2; i--)
  262.             DeleteMenuItem(menuHdl, i);
  263.  
  264.         popup =  (FW_CPopupMenu*)FindViewByID(ev, kPPobPopupID);
  265.         menuHdl = (MenuHandle)popup->GetPlatformMenuHandle();
  266.         numItems = CountMItems(menuHdl);
  267.         for (i = numItems; i > 2; i--)
  268.             DeleteMenuItem(menuHdl, i);
  269.  
  270.         // Call FW_CDialogFrame to close the dialog on OK & Cancel 
  271.         FW_CDialogFrame::HandleNotification(ev, notification);
  272.         
  273.         // Switch the frame's views
  274.         if (viewID != 0)
  275.             part->ChangeViews(ev, viewID);
  276.     }    
  277. }
  278.  
  279.